
// Implementation of a dictionary using a hash table

#include <stdlib.h>
#include <string.h>
#include "hash-dictionary.h"

// Constructor
HashDictionary::HashDictionary()
{
	// Add your code here
}

int
HashDictionary::hash(KeyType key) {
	// Add your code here
	int sum = 0;
	int len = strlen(key);
	for (int i = 0; i < len; i++) {
		sum += i*key[i];
	}
	return sum%MaxBuckets;
}

// Add a record to the dictionary. Returns false if key already exists
bool
HashDictionary::addRecord( KeyType key, DataType record)
{
	int h = hash(key);
	HashNode *temp = buckets[h];
	HashNode *prev;
	while (temp != NULL && strcmp(key, temp->key)) {
		prev = temp;
		temp = temp->next;
	}
	if (temp == NULL) {
		temp = new Node;
		prev->next = temp;
		temp->data = record;
		temp->key = strdup(key);
		temp->next = NULL;
	}

	return false;
}

// Find a key in the dictionary and return corresponding record or NULL
DataType
HashDictionary::findRecord( KeyType key)
{
	return NULL;
}

// Removes one element from th08e table
bool
HashDictionary::removeElement(KeyType key)
{
	if (findRecord(key) == NULL)
		return false;
	int h = hash(key);
	HashNode *temp = buckets[h];
	HashNode *prev;
	while (temp != NULL && strcmp(key, temp->key)) {
		prev = temp;
		temp = temp->next;
	}

	return true;
}

// Returns all the elements in the table as an array of strings.
// *n is the size of the table and it is returned by reference
KeyType *
HashDictionary::keys(int * n)
{
	KeyType * a = (KeyType *) malloc(nElements * sizeof(KeyType));

	*n = nElements;
	int i = 0;
	for (int h = 0; h < MaxBuckets; h++) {
		HashNode * n = buckets[h];
		while (n!=NULL) {
			a[i] = n->key;
			i++;
			n = n->next;
		}
	}
	*n = nElements;
	return a;
}

